<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('主持人管理')"/>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="post-form">
                <div class="select-list">
                    <ul>
                        <li>
                            姓名：<input type="text" name="hostName"/>
                        </li>
                        <li>
                            账号状态：
                            <select name="hostState" th:with="type=${@dict.getType('sys_normal_disable')}">
                                <option value="-1">请选择</option>
                                <option value="1">正常</option>
                                <option value="2">禁用</option>
                            </select>
                        </li>
                        <li>
                            订单量排序：
                            <select name="hostOrderNum" th:with="type=${@dict.getType('sys_normal_disable')}">
                                <option value="-1">请选择</option>
                                <option value="1">订单量降序</option>
                                <option value="2">订单量升序</option>
                                <option value="3">权重降序</option>
                                <option value="4">权重升序</option>
                            </select>
                        </li>
                        <li>
                            星推荐：
                            <select name="hostRecommend" th:with="type=${@dict.getType('sys_normal_disable')}">
                                <option value="-1">请选择</option>
                                <option value="1">推荐</option>
                                <option value="0">不推荐</option>
                            </select>
                        </li>
                        <li>
                            折扣：
                            <select th:with="type=${@dict.getType('sys_normal_disable')}">
                                <option value="-1">请选择</option>
                                <option value="1">有折扣</option>
                                <option value="0">无折扣</option>
                            </select>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i
                                    class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i
                                    class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="btn-group-sm" id="toolbar" role="group">
            <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="user:host:add">
                <i class="fa fa-plus"></i> 新增
            </a>
            <a class="btn btn-primary single disabled" onclick="$.operate.edit()"
               shiro:hasPermission="user:host:edit">
                <i class="fa fa-edit"></i> 修改
            </a>
            <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()"
               shiro:hasPermission="user:host:remove">
                <i class="fa fa-remove"></i> 删除
            </a>
            <a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="user:host:export">
                <i class="fa fa-download"></i> 导出
            </a>
        </div>

        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>
<th:block th:include="include :: footer"/>
<script th:inline="javascript">
    var editFlag = [[${@permission.hasPermi('user:host:edit')}]];
    var removeFlag = [[${@permission.hasPermi('user:host:remove')}]];
    var disableFlag = [[${@permission.hasPermi('user:host:state')}]];
    var datas = [[${@dict.getType('sys_normal_disable')}]];
    var prefix = ctx + "user/host";

    $(function () {
        var options = {
            url: prefix + "/list",
            createUrl: prefix + "/add",
            updateUrl: prefix + "/edit/{id}",
            removeUrl: prefix + "/remove",
            exportUrl: prefix + "/export",
            sortName: "hostSortWeight",
            modalName: "主持人",
            sortOrder: "desc",
            columns: [{
                checkbox: true
            },
                {
                    field: 'hostId',
                    title: '主键'
                },
                {
                    field: 'hostSortWeight',
                    title: '权重',
                    formatter: function (value, row, index) {
                        return "<input class='host-sort-weight' type='text' size='3'  onblur='sortWeightblur(this)' onfocus='sortWeightFocus(this)' value='" + value + "' />"
                    }
                },
                {
                    field: 'hostName',
                    title: '姓名'
                },
                {
                    field: 'hostPhone',
                    title: '手机号'
                },
                {
                    field: 'hostRegtime',
                    title: '注册时间',
                    sortable: true
                },
                {
                    field: 'hostPrice',
                    title: '价格',
                    sortable: true
                },
                {
                    //field: 'hostPrice',
                    title: '订单量',
                    sortable: true
                },
                {
                    field: 'discount',
                    title: '折扣',
                    sortable: true
                },
                {
                    field: 'hostRecommend',
                    title: '星推荐',
                    formatter: function (value, row, index) {
                        if (value == 0) {
                            return "<span style='color:red;'>否</span>";
                        }
                        return "是";
                    }
                },
                {
                    field: 'hostState',
                    title: '账号状态',
                    formatter: function (value, row, index) {
                        if (value == 2) {
                            return "<span style='color:red;'>禁用</span>";
                    }
                        return "正常";
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="permissionSet('+row.hostId+')">权限设置</a> ');
                        if (row.hostState == 2) {
                            actions.push('<a class="btn btn-info btn-xs ' + disableFlag + '" href="javascript:void(0)" onclick="updateState(this,' + row.hostId + ',' + row.hostState + ')">启用账号</a>');
                        } else {
                            actions.push('<a class="btn btn-danger btn-xs ' + disableFlag + '" href="javascript:void(0)" onclick="updateState(this,' + row.hostId + ',' + row.hostState + ')">禁用账号</a>');
                        }
                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);

    });


    function permissionSet(hostId) {
        $.modal.open("权限设置", "/user/host/permissionset/"+hostId);
    }

    // 修改状态，禁用和启用
    function updateState(obj, hostId, hostState) {
        if (hostState == 2) {
            hostState = 1;
        } else {
            hostState = 2;
        }
        $.post(prefix + "/updateState", {"hostId": hostId, "hostState": hostState}, function (data) {
            if (data.code == 0) {
                $.table.refresh();
            } else {
                $.modal.msgSuccess("操作失败");
            }
        }, "json");
    }

    var oldValue = 100;

    //权重获取焦点
    function sortWeightFocus(obj) {
        oldValue = $(obj).val();
    }

    //权重失去焦点
    function sortWeightblur(obj) {

        var weight = $(obj).val();
        if (isNaN(weight) == false) {
            var weightInt = parseInt(weight);
            if (weightInt == parseFloat(weight)) {
                if (weightInt >= 0 && weightInt <= 100) {

                    var hostId = $(obj).parent().prev().text();
                    $.post(prefix + "/update/sortweight", {
                        "hostSortWeight": weightInt,
                        "hostId": hostId
                    }, function (data) {
                        if (data.code == 0) {
                            $.table.refresh();
                        }
                    })
                } else {
                    $.modal.alertWarning("只支持0-100的整数");
                    $(obj).val(oldValue);
                }
            } else {
                $.modal.alertWarning("只支持整数");
                $(obj).val(oldValue);
            }
        } else {
            $.modal.alertWarning("请输入数字");
            $(obj).val(oldValue);
        }
    }

</script>
</body>
</html>